﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Lists;
using DevExpress.XtraCharts;


namespace BI
{
    public partial class UcTwoSeriesChart : UserControl
    {

        private DataTable _Data = new DataTable();
        public DataTable Data
        {
            get
            {
                return _Data;
            }
            set
            {
                _Data = value;
            }
        }

        public UcTwoSeriesChart()
        {
            InitializeComponent();

            DataColumn column1 = new DataColumn("Series1Name", System.Type.GetType("System.String"));
            Data.Columns.Add(column1);

            DataColumn column2 = new DataColumn("Series1Value", System.Type.GetType("System.Decimal"));
            Data.Columns.Add(column2);

            DataColumn column3 = new DataColumn("Series2Name", System.Type.GetType("System.String"));
            Data.Columns.Add(column3);

            DataColumn column4 = new DataColumn("Series2Value", System.Type.GetType("System.Decimal"));
            Data.Columns.Add(column4);
        }



        public void GenChart(string series1Name, string series2Name, ViewType viewType, string title)
        {
            ChartControl chart = new ChartControl();
            Series series1 = new Series(series1Name, viewType);
            chart.Series.Add(series1);
            Series series2 = new Series(series2Name, viewType);
            chart.Series.Add(series2);
            chart.PaletteName = "Aspect";
           
            foreach (DataRow row in _Data.Rows)
            {
                if (row["Series1Value"] is decimal)
                    series1.Points.Add(new SeriesPoint((string)row["Series1Name"] + "_", Convert.ToDouble(row["Series1Value"])));
                else
                    series1.Points.Add(new SeriesPoint((string)row["Series1Name"] + "_", (decimal)0));

                if (row["Series2Value"] is decimal)
                    series2.Points.Add(new SeriesPoint((string)row["Series2Name"] + "_", Convert.ToDouble(row["Series2Value"])));
                else
                    series2.Points.Add(new SeriesPoint((string)row["Series2Name"] + "_", (decimal)0));
            }
            ChartTitle chartTitle = new ChartTitle();
            chartTitle.Text = title;
            chart.Titles.Add(chartTitle);
            chart.Legend.Visible = true;

            for (int i = 0; i < this.Controls.Count; i++) this.Controls.RemoveAt(0);

            chart.Dock = DockStyle.Fill;
            this.Controls.Add(chart);
        }

        public void GenChart(string series1Name, string series2Name, ViewType viewType, string title, string paletteName)
        {
            ChartControl chart = new ChartControl();
            Series series1 = new Series(series1Name, viewType);
            chart.Series.Add(series1);
            Series series2 = new Series(series2Name, viewType);
            chart.Series.Add(series2);
            if (paletteName == "")
                chart.PaletteName = "Aspect";
            else
                chart.PaletteName = paletteName;

            foreach (DataRow row in _Data.Rows)
            {
                if (row["Series1Value"] is decimal)
                    series1.Points.Add(new SeriesPoint((string)row["Series1Name"] + "_", Convert.ToDouble(row["Series1Value"])));
                else
                    series1.Points.Add(new SeriesPoint((string)row["Series1Name"] + "_", (decimal)0));

                if (row["Series2Value"] is decimal)
                    series2.Points.Add(new SeriesPoint((string)row["Series2Name"] + "_", Convert.ToDouble(row["Series2Value"])));
                else
                    series2.Points.Add(new SeriesPoint((string)row["Series2Name"] + "_", (decimal)0));
            }
            ChartTitle chartTitle = new ChartTitle();
            chartTitle.Text = title;
            chart.Titles.Add(chartTitle);
            chart.Legend.Visible = true;

            for (int i = 0; i < this.Controls.Count; i++) this.Controls.RemoveAt(0);

            chart.Dock = DockStyle.Fill;
            this.Controls.Add(chart);
        }
    }
}
